package com.jiangyg.devtools.execute.camelcase;

import com.intellij.openapi.fileTypes.PlainTextLanguage;
import com.intellij.openapi.project.Project;
import com.intellij.ui.EditorTextField;
import com.jiangyg.devtools.toolkit.AwtUtils;
import com.jiangyg.devtools.toolkit.CamelCaseUtils;
import com.jiangyg.devtools.ui.AbstractDialogWrapper;
import com.jiangyg.devtools.ui.EditorTextAreaBuilder;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 类描述：驼峰工具窗口
 *
 * @author jiangyg
 * @date 2023-08-20
 */
public class CamelCaseDialogWrapper extends AbstractDialogWrapper {

    private final EditorTextField textArea;

    public CamelCaseDialogWrapper(@Nullable Project project) {
        super(project);

        this.textArea = EditorTextAreaBuilder.instance()
                .project(project).defaultFeatures().build(PlainTextLanguage.INSTANCE);

        this.setSizeByRatio(0.3, 0.6);

        this.init();
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        return textArea;
    }

    @Override
    protected @NotNull JPanel createButtonsPanel(@NotNull List<? extends JButton> buttons) {
        buttons.clear();
        final List<JButton> buttonList = new ArrayList<>();
        buttonList.add(buildLowerCamelCaseButton());
        buttonList.add(buildUpperCamelCaseButton());
        buttonList.add(buildCompleteButton());
        return super.createButtonsPanel(buttonList);
    }

    private JButton buildLowerCamelCaseButton() {
        final JButton button = new JButton("小驼峰格式化(Ctrl+1)");
        final ActionListener actionListener = e -> doFormateEvent(false);
        button.addActionListener(actionListener);
        button.registerKeyboardAction(actionListener, KeyStroke.getKeyStroke(KeyEvent.VK_1, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
        return button;
    }

    private JButton buildUpperCamelCaseButton() {
        final JButton button = new JButton("大驼峰格式化(Ctrl+2)");
        final ActionListener actionListener = e -> doFormateEvent(true);
        button.addActionListener(actionListener);
        button.registerKeyboardAction(actionListener, KeyStroke.getKeyStroke(KeyEvent.VK_2, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
        return button;
    }

    private JButton buildCompleteButton() {
        final JButton button = new JButton("复制并关闭(Ctrl+3)");
        final ActionListener actionListener = e -> {
            AwtUtils.setClipboardContents(textArea.getText());
            doCancelAction();
        };
        button.addActionListener(actionListener);
        button.registerKeyboardAction(actionListener, KeyStroke.getKeyStroke(KeyEvent.VK_3, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
        return button;
    }

    /**
     * 功能描述：格式化事件
     *
     * @param capitalize 首字母是否大写
     */
    private void doFormateEvent(boolean capitalize) {
        StringReader reader = null;
        BufferedReader buffer = null;
        try {
            final StringBuilder sb = new StringBuilder();

            reader = new StringReader(textArea.getText());
            buffer = new BufferedReader(reader);
            String line;
            while ((line = buffer.readLine()) != null) {
                String str = CamelCaseUtils.underlineToCamel(line);
                if (capitalize) {
                    str = StringUtils.capitalize(str);
                }
                sb.append(str);
                sb.append("\r\n");
            }

            textArea.setText(sb.toString());
        } catch (Exception ex) {
            try {
                IOUtils.close(buffer);
                IOUtils.close(reader);
            } catch (Exception exception) {
                // ignore
            }
        }
    }

}
